<template>
    <div>
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <div class="title">
                    <span>医生个人信息</span>
                </div>
                <div>

                </div>
            </div>
            <div class="text item">
                <el-form ref="form" :model="userInfo" label-width="100px">
                    <el-form-item label="医生头像">
                        <el-upload :http-request="uploadImage" action="" :show-file-list="false" :limit="1"
                            :on-exceed="handleExceed" ref="userAvatar">
                            <img :src="userInfo.avatar" alt="点击修改图片" width="60px">
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="医生账号">
                        <el-input v-model="userInfo.username" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="医生姓名">
                        <el-input v-model="userInfo.name"></el-input>
                    </el-form-item>
                    <el-form-item label="医生简介">
                        <el-input v-model="userInfo.introduce"></el-input>
                    </el-form-item>
                    <el-form-item label="所属医院">
                        <el-input v-model="userInfo.hospitalName" :disabled="true"></el-input>
                    </el-form-item>
                    <el-form-item label="医生职位">
                        <el-select v-model="userInfo.jobId">
                            <el-option v-for="item in jobList" :key="item.name" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="center">
                        <el-button type="primary" @click="onSubmit">保存</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: "DoctorInfo",
    data() {
        return {
        }
    },
    mounted() {
        this.getJobList()
    },
    methods: {
        // 保存用户信息
        async onSubmit() {
            let res = await this.$store.dispatch("uploadDoctorInfo", this.$store.state.user.userInfo);
            if (res.success) this.$message.success(res.message)
            else this.$myfun.resError(res)
        },
        // 上传头像
        async uploadImage(param) {
            const formData = new FormData()
            formData.append('multipartFile', param.file)
            let res = await this.$store.dispatch("uploadDoctorAvatar", formData);
            if (res.success) {
                this.$message.success(res.message)
                this.$store.commit("setUserAvatar", res.data)
            }
            else this.$myfun.resError(res)
        },
        // 超出限制
        handleExceed(files, fileList) {
            this.$message.warning('只能选择一张图片')
        },
        // 获取职位列表
        async getJobList() {
            let res = await this.$store.dispatch("getJobList")
            if (!res.success) this.$myfun.resError(res)
        }
    },
    computed: {
        userInfo() {
            return this.$store.state.user.userInfo
        },
        jobList() {
            return this.$store.state.job.jobList
        }
    }
}
</script>

<style scoped>
.title {
    font-size: 1.1rem;
    font-weight: 550;
}

.center {
    text-align: center;
}
</style>